package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import models.User;
import utils.DAOUser;

/**
 * Servlet implementation class FollowController
 */
public class UserController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();

		synchronized (session) {

			String user = (String) session.getAttribute("user");
			User currentUser = (User) session.getAttribute("currentUser");

			if (request.getParameter("follow") != null) {
				String following = request.getParameter("follow");
				try {
					DAOUser daouser = new DAOUser();
					daouser.followUser(user, following);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else if (request.getParameter("unfollow") != null) {
				String following = request.getParameter("unfollow");
				try {
					DAOUser daouser = new DAOUser();
					daouser.unfollowUser(user, following);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					response.sendRedirect("blank.jsp");
				}
			}

			if (request.getParameter("addContact") != null) {
				String contact = request.getParameter("addContact");
				try {
					DAOUser daouser = new DAOUser();
					daouser.addContact(contact, user);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					response.sendRedirect("blank.jsp");
				}
			} else if (request.getParameter("deleteContact") != null) {
				String contact = request.getParameter("deleteContact");
				try {
					DAOUser daouser = new DAOUser();
					daouser.deleteContact(contact, user);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					response.sendRedirect("blank.jsp");
				}
			}

			if (request.getParameter("deleteUser") != null) {
				try {
					DAOUser daouser;
					daouser = new DAOUser();
					daouser.deleteUser(request.getParameter("deleteUser"));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					response.sendRedirect("blank.jsp");
				}
			}
			// Update currentUser:
			try {
				DAOUser daoUser = new DAOUser();
				daoUser.setAcceptedContact(currentUser, user);
				daoUser.setContact(currentUser, user);
				daoUser.setFollower(currentUser, user);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				response.sendRedirect("blank.jsp");
			}

			// System.out.println(content);
			response.setStatus(200);
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.print("OK");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
